const app = getApp();
Page({
	data: {
		username: '用户',
		userPic: '../../../images/header.jpg'
	},
	onLoad: function () {
		let Uidentity = app.globalData.loginInfo.openId;
		var that = this;
		let baseUrl = app.globalData.baseUrl;
		wx.request({
			url: baseUrl + '/api/user/getqrcode',
			data: {
				Uidentity:Uidentity
			},
			method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
			// header: {}, // 设置请求的 header
			success: function (res) {
				if (res.data.code === 200) {
					that.setData({
						userPic : baseUrl + res.data.data
					});
				}
				// success
			},
			fail: function () {
				// fail
			},
			complete: function () {
				// complete
			}
		});
	},
	saveImage: function () {
		wx.showLoading({
			title: '保存中...'
		});
		wx.downloadFile({
			url: this.data.userPic,
			success: function (res) {
				//图片保存到本地
				wx.saveImageToPhotosAlbum({
					filePath: res.tempFilePath,
					success: function (data) {
						wx.hideLoading();
						wx.showModal({
							title: '提示',
							content: '已经下载',
							showCancel: false,
						});
					},
					fail: function (err) {
						if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
							// 这边微信做过调整，必须要在按钮中触发，因此需要在弹框回调中进行调用
							wx.showModal({
								title: '提示',
								content: '需要您授权保存相册',
								showCancel: false,
								success: modalSuccess => {
									wx.openSetting({
										success(settingdata) {
											console.log("settingdata", settingdata);
											if (settingdata.authSetting['scope.writePhotosAlbum']) {
												wx.showModal({
													title: '提示',
													content: '获取权限成功,再次点击图片即可保存',
													showCancel: false,
												});
											} else {
												wx.showModal({
													title: '提示',
													content: '获取权限失败，将无法保存到相册哦~',
													showCancel: false,
												});
											}
										},
										fail(failData) {
											console.log("failData", failData);
										},
										complete(finishData) {
											console.log("finishData", finishData);
										}
									});
								}
							});
						}
					},
					complete(res) {
						wx.hideLoading();
					}
				});
			}
		});
	}
	// pageBack() {
	// 	wx.navigateBack({
	// 		delta: 1
	// 	});
	// }


});